﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Sbook.AdminIstrator
{
    public partial class AddNews : System.Web.UI.Page
    {
        static string IdNews = "";
        static string IdGroup = "";
        static bool InsertNews = false;
        static bool InsertNewsGroup = false;
        protected string username = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    username = Data.Mysession.Session.Username;
                    BindGridNews();
                    BindGroup();
                    BindGridNewsGroup();
                    BindSelectGroup();
                    InsertNews = false;
                    InsertNewsGroup = false;
                }
                else
                {
                    Response.Redirect("/AdminIstrator/Login.aspx");
                }
            }
        }

        private void BindGridNews()
        {
            grdNews.DataSource = Business.NewsService.News_GetByAll();
            grdNews.DataBind();
            for (int i = 0; i < grdNews.Items.Count; i++)
            {
                List<Data.NewsGroup> listGroup = Business.NewsGroupService.NewsGroup_GetById(grdNews.Items[i].Cells[5].Text);
                grdNews.Items[i].Cells[5].Text = listGroup[0].GroupName;
                if (grdNews.Items[i].Cells[6].Text == "True")
                {
                    grdNews.Items[i].Cells[6].Text = "Hiện";
                    ((LinkButton)grdNews.Items[i].FindControl("lbtActive")).Text = "Ẩn";
                }
                else
                {
                    grdNews.Items[i].Cells[6].Text = "Ẩn";
                    ((LinkButton)grdNews.Items[i].FindControl("lbtActive")).Text = "Hiện";
                }
            }

        }

        private void BindGridNewsGroup()
        {
            grdNewsGroup.DataSource = Business.NewsGroupService.NewsGroup_GetByAll();
            grdNewsGroup.DataBind();
            for (int i = 0; i < grdNewsGroup.Items.Count; i++)
            {
                if (grdNewsGroup.Items[i].Cells[3].Text == "True")
                {
                    grdNewsGroup.Items[i].Cells[3].Text = "Hiện";
                    ((LinkButton)grdNewsGroup.Items[i].FindControl("lbtActive")).Text = "Ẩn";
                }
                else
                {
                    grdNewsGroup.Items[i].Cells[3].Text = "Ẩn";
                    ((LinkButton)grdNewsGroup.Items[i].FindControl("lbtActive")).Text = "Hiện";
                }
            }
        }

        private void BindGroup()
        {
            ddlGroup.Items.Clear();
            ddlGroup.Items.Add(new ListItem("------------ Group News -------------","0"));
            List<Data.NewsGroup> list = Business.NewsGroupService.NewsGroup_GetByAll();
            for (int i = 0; i < list.Count; i++)
            {
                ddlGroup.Items.Add(new ListItem(list[i].GroupName,list[i].GroupID));
            }
            ddlGroup.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ResetFormNews();
            InsertNews = true;
            pnViewNews.Visible = false;
            pnAddNews.Visible = true;
        }

        private string Save(FileUpload file)
        {
            string math = "";
            if (file.FileName != "")
            {
                math = Server.MapPath("/images/img_tintuc/" + file.FileName);
                file.SaveAs(math);
                return "/images/img_tintuc/" + file.FileName.ToString();
            }
            else
                return "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Data.NewsInfo objNews = new Data.NewsInfo();

                objNews.nTitle = txtTitle.Text;
                objNews.nDescription = txtDescription.Text;
                objNews.nContent = fckContent.Value;
                objNews.nStatus = (chkStatus.Checked) ? "1" : "0";
                objNews.groupID = ddlGroup.SelectedValue;
                //objNews.Image = fuImage.FileName;
                if (objNews.groupID == "0")
                {
                    ShowPopUpMsg("Bạn chưa chọn nhóm tin!");
                    return;
                }
                if (InsertNews == true)
                {
                    objNews.Image = Save(fuImage);
                    Business.NewsService.News_Insert(objNews);
                }
                else
                {
                    if (Save(fuImage) != "")
                    {
                        objNews.Image = Save(fuImage);
                    }
                    else
                    {
                        objNews.Image = lblFile.Text;
                    }
                    objNews.nID = IdNews;
                    Business.NewsService.News_Update(objNews); 
                }

                BindGridNews();
                pnAddNews.Visible = false;
                pnViewNews.Visible = true;
            }
        }

        private void ResetFormNews()
        {
            txtTitle.Text = "";
            txtDescription.Text = "";
            fckContent.Value = null;
            chkStatus.Checked = false;
            BindGroup();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            pnViewNews.Visible = true;
            pnAddNews.Visible = false;
            
            BindGridNews();
        }

        protected void grdNews_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    IdNews = strCA;
                    InsertNews = false;
                    List<Data.NewsInfo> objNews = Business.NewsService.News_GetById(IdNews);
                    txtTitle.Text = objNews[0].nTitle;
                    txtDescription.Text = objNews[0].nDescription;
                    fckContent.Value = objNews[0].nContent;
                    chkStatus.Checked = (objNews[0].nStatus == "True") ? true : false;
                    lblFile.Text = objNews[0].Image;
                    pnAddNews.Visible = true;
                    pnViewNews.Visible = false;
                    ddlGroup.SelectedValue = objNews[0].groupID;
                    BindGroup();
                    break;
                case "Delete":
                    Business.NewsService.News_Delete(strCA);
                    BindGridNews();
                    break;
                case "Active":
                    string status = "";
                    if (e.Item.Cells[6].Text == "Hiện")
                    {
                        status = "0";
                    }
                    else
                    {
                        status = "1";
                    }
                    Business.NewsService.News_UpdateStatus(strCA, status);
                    BindGridNews();
                    break;   

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (grdNews.Items.Count > 0)
            {
                
                for (int i = 0; i < grdNews.Items.Count; i++)
                {
                    CheckBox chk = (CheckBox)grdNews.Items[i].FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        string Id = grdNews.Items[i].Cells[1].Text;
                        Business.NewsService.News_Delete(Id);
                    }
                }
                BindGridNews();
            }
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void lbtEditNews_Click(object sender, EventArgs e)
        {
            pnViewNews.Visible = true;
            pnAddNews.Visible = false;
            pnViewNewsGroup.Visible = false;
            pnAddNewsGroup.Visible = false;
            BindGridNews();
        }

        protected void lbtEditNewsGroup_Click(object sender, EventArgs e)
        {
            pnViewNews.Visible = false;
            pnAddNews.Visible = false;
            pnViewNewsGroup.Visible = true;
            pnAddNewsGroup.Visible = false;
            BindGridNewsGroup();
        }

        protected void grdNewsGroup_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    IdGroup = strCA;
                    InsertNewsGroup = false;
                    List<Data.NewsGroup> objGroup = Business.NewsGroupService.NewsGroup_GetById(IdGroup);
                    txtGroupName.Text = objGroup[0].GroupName;
                    chkGStatus.Checked = objGroup[0].GStatus == "True" ? true : false;
                    pnAddNewsGroup.Visible = true;
                    pnViewNewsGroup.Visible = false;
                    
                    break;
                case "Delete":
                    Business.NewsGroupService.NewsGroup_DeleteById(strCA);
                    BindGridNewsGroup();
                    break;
                case "Active":
                    string status = "";
                    if (e.Item.Cells[3].Text == "Hiện")
                    {
                        status = "0";
                    }
                    else
                    {
                        status = "1";
                    }
                    Business.NewsGroupService.NewsGroup_UpdateStatusById(strCA, status);
                    BindGridNewsGroup();
                    break;
            }
        }

        protected void btnAddNewsGroup_Click(object sender, EventArgs e)
        {
            txtGroupName.Text = "";
            chkGStatus.Checked = false;
            pnViewNewsGroup.Visible = false;
            pnAddNewsGroup.Visible = true;
            InsertNewsGroup = true;
        }

        protected void btnSaveGroup_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Data.NewsGroup group = new Data.NewsGroup();
                group.GroupName = txtGroupName.Text;
                group.GStatus = chkGStatus.Checked ? "1" : "0";
                if (InsertNewsGroup)
                {
                    Business.NewsGroupService.NewsGroup_Insert(group);

                }
                else
                {
                    group.GroupID = IdGroup;
                    Business.NewsGroupService.NewsGroup_Update(group);
                }
                BindGridNewsGroup();
                pnAddNewsGroup.Visible = false;
                pnViewNewsGroup.Visible = true;
            }
        }

        protected void btnDeleteNewsGroup_Click(object sender, EventArgs e)
        {
            if (grdNewsGroup.Items.Count > 0)
            {
                for (int i = 0; i < grdNewsGroup.Items.Count; i++)
                {
                    CheckBox chk = (CheckBox)grdNewsGroup.Items[i].FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        Business.NewsGroupService.NewsGroup_DeleteById(grdNewsGroup.Items[i].Cells[1].Text);
                    }
                }
                BindGridNewsGroup();
            }
        }

        private void BindSelectGroup()
        {
            ddlSelectGroup.Items.Clear();
            ddlSelectGroup.Items.Add(new ListItem("Tất cả","0"));
            List<Data.NewsGroup> list = Business.NewsGroupService.NewsGroup_GetByAll();
            for (int i = 0; i < list.Count; i++)
            {
                ddlSelectGroup.Items.Add(new ListItem(list[i].GroupName,list[i].GroupID));
            }
            ddlSelectGroup.DataBind();
        }

        protected void grdNews_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdNews.CurrentPageIndex = e.NewPageIndex;
            BindGridNews();
        }

        protected void grdNewsGroup_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdNewsGroup.CurrentPageIndex = e.NewPageIndex;
            BindGridNewsGroup();
        }

        protected void ddlSelectGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSelectGroup.SelectedIndex == 0)
            {
                BindGridNews();
                return;
            }
            BindGridNewsByGroupId(ddlSelectGroup.SelectedValue);
        }

        private void BindGridNewsByGroupId(string Id)
        {
            
            grdNews.DataSource = Business.NewsService.News_GetByTop("","GroupID="+Id,"NDate desc");
            grdNews.DataBind();
            for (int i = 0; i < grdNews.Items.Count; i++)
            {
                List<Data.NewsGroup> listGroup = Business.NewsGroupService.NewsGroup_GetById(grdNews.Items[i].Cells[5].Text);
                grdNews.Items[i].Cells[5].Text = listGroup[0].GroupName;
                if (grdNews.Items[i].Cells[6].Text == "True")
                {
                    grdNews.Items[i].Cells[6].Text = "Hiện";
                    ((LinkButton)grdNews.Items[i].FindControl("lbtActive")).Text = "Ẩn";
                }
                else
                {
                    grdNews.Items[i].Cells[6].Text = "Ẩn";
                    ((LinkButton)grdNews.Items[i].FindControl("lbtActive")).Text = "Hiện";
                }
            }
        }
       
    }
}